package cn.kow.kmall.controller.admin;

import cn.kow.kmall.admin.pojo.dto.OptionDTO;
import cn.kow.kmall.common.enums.*;
import cn.kow.kmall.controller.BaseController;
import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.pojo.entity.ItemCategory;
import cn.kow.kmall.service.ItemCategoryService;
import cn.kow.kmall.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-05-13 15:27
 * @Modified By:
 */
@Slf4j
@RestController
public class OptionController extends BaseController {

    /**
     * 商品服务
     */
    @Autowired
    private ItemService itemService;

    /**
     * 分类服务
     */
    @Autowired
    private ItemCategoryService categoryService;

    @GetMapping("/option/{type}")
    public KmallResult getOption(@PathVariable String type) {
        if ("item-status".equals(type)) {
            return covertMap2Response(ItemStatusEnum.getMap());
        } else if ("item-category".equals(type)) {
            return categoryOptions();
        } else if ("order-status".equals(type)) {
            return covertMap2Response(OrderStatusEnum.getMap());
        } else if ("gender".equals(type)) {
            return covertMap2Response(UserGenderEnum.getMap());
        } else if ("service-status".equals(type)) {
            return covertMap2Response(OrderServiceStatusEnum.getMap());
        } else if ("service-type".equals(type)) {
            return covertMap2Response(OrderServiceTypeEnum.getMap());
        } else if ("link-type".equals(type)) {
            return covertMap2Response(ContentLinkTypeEnum.getMap());
        } else if ("module-type".equals(type)) {
            return covertMap2Response(ContentModuleTypeEnum.getMap());
        } else if ("status".equals(type)) {
            return covertMap2Response(KmallStatusEnum.getMap());
        }
        return KmallResult.success();
    }

    private KmallResult categoryOptions() {
        // 所有的一级类目
        List<ItemCategory> superList = categoryService.listByParentId(0L);
        List<OptionDTO> options = new ArrayList<>(superList.size());
        for(ItemCategory category : superList) {
            List<ItemCategory> subList = categoryService.listByParentId(category.getId());
            options.add(
                    OptionDTO.builder()
                            .key(String.valueOf(category.getId()))
                            .value(category.getName())
                            .children(subList.stream()
                                    .map(c -> OptionDTO.builder().key(String.valueOf(c.getId())).value(c.getName()).build()).collect(Collectors.toList()))
                            .build()
            );
        }
        return KmallResult.success().put("options", options);
    }

    /**
     * 将map转成响应接口
     *
     * @param map 状态map
     * @return KmallResult
     */
    public KmallResult covertMap2Response(Map<?, String> map) {
        if (MapUtils.isEmpty(map)) {
            return KmallResult.fail();
        }
        List<OptionDTO> list = new ArrayList<>(map.size());
        map.forEach((k, v) -> {
            list.add(OptionDTO.builder().key(String.valueOf(k)).value(v).build());
        });
        return KmallResult.success().put("options", list);
    }

}
